<?php
/**
 * 登录控制器
 * @Author    紫梦
 * @upDate    2018-01-30
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\common\model\RolesNode;

class Login extends controller
{
	public function login()
	{
		$request = Request::instance();
		if($request->isAjax()){
			$data = $request->post();
			$admin = Db::name('admin')->field('id,agency_id,password,nick_name,role_id')->where(['user_name'=>$data['user_name'],'status'=>1])->find();
			if($admin){
				$pass = password_verify($data['password'],$admin['password']);
				if($pass){
					unset($admin['password']);
					$rolesNode = RolesNode::where(['role_id'=>$admin['role_id']])->select();
					if($rolesNode){
						foreach ($rolesNode as $value) {
							$nodes['nodes'][] = strtolower($value['nodes']['node_value']);
						}					
					}else{
						$nodes['nodes'][] = [];
					}
					$session = array_merge($admin,$nodes);
					session('admin',$session);
				}else{
					return [
						'code'=> 2,
						'msg'=> "密码错误"
						];
				}
			}else{
				return [
					'code'=> 2,
					'msg'=> "账号不存在"
					];
			}
			return [
                'code'=>1,
                'msg'=>'登录成功'
                ];

		}else{
			return $this->fetch();
		}
	}

	public function loginOut()
	{
		//退出登录
		session(null);
		return redirect('index/index/index');
	}
}